library(qualtRics)
library(dplyr)
library(estimatr)
library(tidyr)
library(ggplot2)
library(magrittr)
library(broom)
library(hrbrthemes)
library(modelsummary)
library(stargazer)

study5 <- qualtRics::read_survey("data/study5.csv") %>%
 mutate(attrit = I(Progress!=100))

study5 %>%
  filter(moderation_failure == 0 & DistributionChannel != "preview", !is.na(treatment)) %>%
 dplyr::select(birthyr, educ, income, ideology = ideology...26, issue,
         canned, generic_flag, contains("polsoph"),
         likert = post_outcome_1, certainty = post_certainty_19,
         mvc_check, pid3, pidlean, pidstrength,
         treatment, contains("pw2p"), contains("participation"), attrit) %>%
  mutate(age = 2023-as.numeric(birthyr),
         educ = educ,
         income = income,
         ideology,
         likert,
         certainty,
         treatment) -> study5_coded

study5_coded %<>%
  mutate(polsoph1 = as.numeric(polsoph1 == 1),
         polsoph2 = as.numeric(polsoph2 == 1),
         polsoph3 = as.numeric(grepl("2/3|thirds|67", polsoph3, ignore.case = T)),
         polsoph4 = as.numeric(grepl("court|judicial|scotus", polsoph4, ignore.case = T)),
         polsoph5 = as.numeric(grepl("vice|vp", polsoph5, ignore.case = T)))

study5_coded$pk_scale <- study5_coded %>% dplyr::select(contains("polsoph")) %>% rowSums()/5

# Create a 7-point partisanship scale
study5_coded %<>%
  mutate(
    pid7 = case_when(
      # Strong Democrat
      pid3 == 1 & pidstrength == 2 ~ 1,
      # Weak Democrat
      pid3 == 1 & pidstrength == 4 ~ 2,
      # Lean Democrat
      pid3 == 3 & pidlean == 2 ~ 3,
      # Independent / No lean
      pid3 == 3 & pidlean == 3 ~ 4,
      # Lean Republican
      pid3 == 3 & pidlean == 1 ~ 5,
      # Weak Republican
      pid3 == 2 & pidstrength == 4 ~ 6,
      # Strong Republican
      pid3 == 2 & pidstrength == 2 ~ 7,
      TRUE ~ NA_real_
    )
  )

study5_coded %>%
  mutate(pw2p_scale = (abbreviated_pw2p_1+
                         abbreviated_pw2p_2+
                         abbreviated_pw2p_3+
                         abbreviated_pw2p_4+
                         abbreviated_pw2p_5+
                         (8-abbreviated_pw2p_6)+
                         abbreviated_pw2p_7+
                         abbreviated_pw2p_8)/8,
         pw2p_defend = ((8-pw2p_defend_abbrev_1) +
                          pw2p_defend_abbrev_2 +
                          (8-pw2p_defend_abbrev_3)+
                          pw2p_defend_abbrev_4+
                          (8-pw2p_defend_abbrev_5))/5)-> study5_coded

study5_coded$participation_scale <- psych::alpha(study5_coded %>%
                                                 dplyr::select(contains("participation")))$scores

minmax <- function(x) ((x - min(x,na.rm=T))/(max(x,na.rm=T) - min(x,na.rm=T)))

summary(lm_lin(pw2p_scale ~ treatment, ~ likert + certainty +
                 age + educ + income + ideology + participation_scale +
                 pk_scale + pid7, study5_coded,
               subset = is.na(generic_flag)))

summary(lm_lin(pw2p_defend ~ treatment, ~ likert + certainty +
                 age + educ + income + ideology + participation_scale + pk_scale +
                 pid7, study5_coded, subset = is.na(generic_flag)))

# Consistent with PAP
model1 <- lm_robust(pw2p_scale ~ as.factor(treatment) + likert + certainty +
                age + educ + income + ideology + participation_scale +
                pk_scale + pid7, study5_coded,
               subset = is.na(generic_flag))

model1b <- lm_robust(pw2p_scale ~ as.factor(treatment), study5_coded,
             subset = is.na(generic_flag))


model2 <- lm_robust(pw2p_defend ~ as.factor(treatment) + likert + certainty +
                age + educ + income + ideology + participation_scale + pk_scale +
                pid7, study5_coded, subset = is.na(generic_flag))

model2b <- lm_robust(pw2p_defend ~ as.factor(treatment),
                     study5_coded, subset = is.na(generic_flag))


texreg::texreg(list(model1, model1b, model2, model2b),
               include.ci = FALSE)


# Define theme parameters
theme_params <- theme(
  legend.position = "none",
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  axis.text.x = element_blank()
)
